﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace MatrixWebApiCore.Filter
{
    /// <summary>
    /// token身份验证
    /// </summary>
    public class TokenFilter : Attribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            Microsoft.Extensions.Primitives.StringValues authorValue;
            bool _authorzation = context.HttpContext.Request.Headers.TryGetValue("Authorization", out authorValue);
            if (_authorzation == false)
            {
                ContentResult result = new ContentResult();
                result.Content = "{\"Success\":false,\"msg\":\"Authorization不能为空\",\"Data\":null}";
                context.Result = result;
            }
        }
    }
}